package com.wmh.baseservice.robot.controller;


import com.wmh.baseservice.common.utils.LogUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.robot.entity.RobotApiUri;
import com.wmh.baseservice.robot.pojo.vo.RegRobotReqVO;
import com.wmh.baseservice.robot.pojo.vo.RobotUpdateStatusReqVO;
import com.wmh.baseservice.robot.pojo.vo.TaskStatementAppendResultReqVO;
import com.wmh.baseservice.robot.service.RobotInfoService;
import com.wmh.baseservice.robot.service.RobotTaskStatementService;
import com.wmh.baseservice.robot.service.RobotTaskStatementV2Service;
import com.wmh.baseservice.robot.tools.MyHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;
import java.util.regex.Matcher;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author 青石
 */
@Api(tags = "自动化机器-公开接口(供机器调用)", value="公开接口(供机器调用)")
@RestController
@RequestMapping("public/robot")
@Validated
@Slf4j
public class PublicRobotController {


    @Resource
    private HttpServletRequest request;

    @Resource
    private RobotInfoService robotInfoService;

    @Resource
    private Map<String, RobotApiUri> robotApiMap;

    @Resource
    private RobotTaskStatementService robotTaskStatementService;

    @Resource
    private RobotTaskStatementV2Service robotTaskStatementV2Service;

    @ApiOperation(value = "获取机器人接口Uri",tags = "公开接口")
    @GetMapping("/getApiUri")
    public String getApiUri() {
        LogUtil.info("getApiUri => robotApiMap : {}", robotApiMap);
        return success(robotApiMap);
    }

    @ApiOperation(value = "注册机器",tags = "公开接口")
    @PostMapping("reg")
    public String reg(RegRobotReqVO reqVO) {
        String number = robotInfoService.reg(reqVO);
        return success(number);
    }

    @ApiOperation(value = "修改机器状态",tags = "公开接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "robotNumber", value = "机器编号", required = true),
            @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "statusId", value = "状态ID",example = "1闲置,2运行,3掉线,4运行中",required = true),
            @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "taskId", value = "任务ID"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "taskName", value = "任务名称"),
    })
    @PostMapping("updateStatus")
    public String updateStatus(RobotUpdateStatusReqVO reqVO) {
        robotInfoService.updateStatus(reqVO);
        return success();
    }

    @PostMapping("updateStatusV2")
    public String updateStatusV2(@Valid RobotUpdateStatusReqVO reqVO){
        robotInfoService.updateStatusV2(reqVO);
        return success();
    }

    @ApiOperation(value = "追加任务报表结果信息",tags = "公开接口")
    @PostMapping("statement/appendResult")
    public String appendTaskStatementResult(@Validated TaskStatementAppendResultReqVO reqVO){
        robotTaskStatementService.appendResult(reqVO);
        return success();
    }

    @ApiOperation(value = "追加任务报表结果信息v2",tags = "公开接口")
    @PostMapping("statement/appendResultV2")
    public String appendTaskStatementResultV2(@Validated TaskStatementAppendResultReqVO reqVO){
        robotTaskStatementV2Service.appendResult(reqVO);
        return success();
    }

    @ApiOperation(value = "后端机器测试接口",tags = "公开接口")
    @PostMapping("test")
    public String test(Integer test){
        log.info("{}",request.getHeader("Content-Type"));
        log.info("{}",test);
        return success(test);
    }

    /**
     * 处理本接口请求机器时的异常
     */
    @ExceptionHandler
    public String handlerException(HttpClientErrorException e) {
        String message = "";
        if (e.getMessage() != null) {
            Matcher matcher = MyHttpUtil.ERR_CODE_PATTERN.matcher(e.getMessage());
            if (matcher.find()) {
                message = matcher.group(1);
            }
        }
        return ReturnBody.error("服务器访问机器接口失败 -> " + message);
    }


}
